package com.three.crm.location;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.amap.api.location.AMapLocation;
import com.three.crm.constant.AppConstant;

public class LocationService extends Service {

    private AmapLocationUtil locationUtil = null;

    public LocationService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("TAG","0000000000");
        initLocation();

        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int interval = 60 * 1000 * 2; //15分钟
        long triggerAtTime = System.currentTimeMillis();
        Intent locationIntent = new Intent(this, LocationReceiver.class);
        locationIntent.putExtra(AppConstant.INDEX,"location");
        PendingIntent pi = PendingIntent.getBroadcast(this,0,locationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
        manager.setRepeating(AlarmManager.RTC_WAKEUP,triggerAtTime,interval,pi);

//        Intent callLogIntent = new Intent(this, LocationReceiver.class);
//        callLogIntent.putExtra(AppConstant.INDEX,"calllog");
//        PendingIntent pendingCallLogIntent = PendingIntent.getBroadcast(this,1,callLogIntent,PendingIntent.FLAG_CANCEL_CURRENT);
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.HOUR,24);
//        calendar.set(Calendar.HOUR_OF_DAY, 22);
//        calendar.set(Calendar.MINUTE, 0);
//        calendar.set(Calendar.SECOND, 0);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0及以上
//            manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
//                    calendar.getTimeInMillis(), pendingCallLogIntent);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
//            manager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
//                    pendingCallLogIntent);
//        }


       // manager.setRepeating(AlarmManager.RTC_WAKEUP,triggerAtTime,3 * 60 * 1000,pendingCallLogIntent);

      //  locationUtil = new AmapLocationUtil(getApplicationContext());
//        if (Build.VERSION.SDK_INT >=  Build.VERSION_CODES.O) {
//            String NOTIFICATION_CHANNEL_ID = "package_name";
//            String channelName = "My Background Service";
//            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,channelName, NotificationManager.IMPORTANCE_LOW);
//            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//            manager.createNotificationChannel(channel);
//            Notification notification = new Notification.Builder(this,NOTIFICATION_CHANNEL_ID)
//                    .setSmallIcon(R.mipmap.ic_logo)  // the status icon
//                    .setWhen(System.currentTimeMillis())  // the time stamp
//                    .setContentText("定时服务正在运行")  // the contents of the entry
//                    .build();
//
//            startForeground(2, notification);
//        }
    }

    private void initLocation() {
        if (locationUtil == null) {
            locationUtil = new AmapLocationUtil(getApplicationContext());
        }
        locationUtil.initLocation();
        locationUtil.setOnCallBackListener(new AmapLocationUtil.onCallBackListener() {
            @Override
            public void onCallBack(double longitude, double latitude, AMapLocation location, boolean isSucdess, String address) {
                if (isSucdess){
                    LocationUtils.getInstance().onUploadLocation(latitude,longitude);
                }
            }
        });
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //return null;
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void unbindService(ServiceConnection conn) {
        super.unbindService(conn);
       // locationUtil.destroyLocation();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Intent i = new Intent(this, LocationService.class);
        startService(i);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("TAG","模拟后台操作");
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (locationUtil == null) {
                    initLocation();
                }
                locationUtil.startLocation();
            }
        }).start();
//        if (intent != null && intent.getExtras() != null) {
//            Log.i("TAG","模拟后台操作=" + flags + ",startId=" +startId + ",555=" + intent.getExtras().getString(AppConstant.INDEX));
//            String index = intent.getExtras().getString(AppConstant.INDEX);
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    if (index.equals("location")) {
//                        if (locationUtil == null) {
//                            initLocation();
//                        }
//                        locationUtil.startLocation();
//                    } else if (index.equals("calllog")){
//                        handleCalllog();
//                    }
//
//                }
//            }).start();
//
//        }

        return super.onStartCommand(intent, flags, startId);
    }

    private void handleCalllog() {
        Log.i("TAG","上传通话记录");
    }

}
